package com.dongliu.controller;

import com.dongliu.domain.LentRecord;
import com.dongliu.domain.User;
import com.dongliu.service.LentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/lentRecord")
public class LentRecordController {
    @Autowired
    private LentRecordService lentRecordService;

    @PutMapping
    public Result returnBook(@RequestBody LentRecord lentRecord) {
        lentRecord = lentRecordService.returnBook(lentRecord);
        Integer code = lentRecord != null ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg = lentRecord != null ? "":"还书失败，请重试！";
        return new Result(code, lentRecord, msg);
    }

    @GetMapping("/{username}")
    public Result getAllByUsername(@PathVariable String username) {
        List<LentRecord> lentRecordList = lentRecordService.getAllByUsername(username);
        Integer code = lentRecordList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = lentRecordList != null ? "":"数据查询失败，请重试！";
        return new Result(code, lentRecordList, msg);
    }

    @GetMapping("/not/{username}")
    public Result getNotReturnByUsername(@PathVariable String username) {
        List<LentRecord> lentRecordList = lentRecordService.getNotReturnByUsername(username);
        Integer code = lentRecordList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = lentRecordList != null ? "":"数据查询失败，请重试！";
        return new Result(code, lentRecordList, msg);
    }

}
